package com.plugin.sproutcore.ui.wizards;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

import com.plugin.sproutcore.lang.SproutCoreProcess;
import com.plugin.sproutcore.util.ProjectUtil;

public class NewModelWizard extends Wizard implements INewWizard 
{
	private IStructuredSelection selection;
	private NewModelWizardPage page;
	
	public NewModelWizard()
	{
		super();
		setNeedsProgressMonitor(true);
	}
	

	public void addPages() {
		page = new NewModelWizardPage(selection);
		addPage(page);
	}

	@Override
	public boolean performFinish() 
	{
		final String className = page.getFullClassName();
		final String appName   = page.getAppName();
		IRunnableWithProgress op = new IRunnableWithProgress() 
		{
			public void run(IProgressMonitor monitor) throws InvocationTargetException 
			{
				try 
				{
					doFinish(className, appName, monitor);
				} 
				catch (CoreException e)
				{
					throw new InvocationTargetException(e);
				} 
				finally 
				{
					monitor.done();
				}
			}
		};
		try 
		{
			getContainer().run(true, false, op);
		} 
		catch (InterruptedException e) 
		{
			return false;
		} 
		catch (InvocationTargetException e) 
		{
			Throwable realException = e.getTargetException();
			MessageDialog.openError(getShell(), "Error", realException.getMessage());
			return false;
		}
		return true;
	}
	
	private void doFinish( String className, String appName, IProgressMonitor monitor )
	throws CoreException 
	{
		// create a sample file
		monitor.beginTask("Creating " + className, 10);
		
		List<String> command = new ArrayList<String>();
		command.add("sc-gen");
		command.add("model");
		command.add(className);
		command.add("--target="+appName);
		
		IProject project = null;
		
		try 
		{
			if (!selection.isEmpty())
			{
				Object element = selection.getFirstElement();
				if (element instanceof IResource) {
					project = ((IResource)element).getProject();
				}
			}
			File dir  = new File(project.getLocation().toOSString());
			SproutCoreProcess.execute(command, dir, monitor);
			
			IPath appPath = new Path("apps/"+appName);
			IFolder appFolder = project.getFolder(appPath);
			if (appFolder.exists()) {
				appFolder.refreshLocal(IProject.DEPTH_INFINITE, monitor);
			}
		} 
		catch (Exception e) 
		{
			IStatus status = ProjectUtil.createErrorStatus(e.getMessage());
			throw new CoreException(status);
		} 
	}

	public void init(IWorkbench workbench, IStructuredSelection selection) 
	{
		this.selection = selection;
	}

}
